import React, { Component } from 'react';
import Qiniu from 'react-qiniu';
import {browserHistory,hashHistory,Link} from 'react-router';
import {SingleInput,DateInput} from './common/Forminput';
import { connect } from 'react-redux';
import { InvoiceUpdate } from '../Redux/Actions/invoiceAction';
import SweetAlert from 'react-bootstrap-sweetalert';

const history = process.env.NODE_ENV !== 'production' ? browserHistory : hashHistory;

class InvoiceUpdateAll extends Component {
  constructor(props){
    super(props);
    this.state = {
      alertstate:"tohide",
      remainInvoice:props.invoiceReducer.invoice.data?props.invoiceReducer.invoice.data.remainInvoice:"0",
      latestMakeInvoiceTimeOfThisMonth:props.invoiceReducer.invoice.data?props.invoiceReducer.invoice.data.latestMakeInvoiceTimeOfThisMonth:null,
      latestMakeInvoiceTimeOfNextMonth:props.invoiceReducer.invoice.data?props.invoiceReducer.invoice.data.latestMakeInvoiceTimeOfNextMonth:null
    };
    this.handlesubmit = (event)=>{
      event.preventDefault(); 
      const token = this.props.authReducer.user.data.token;
      const opt ={
        remainInvoice:this.state.remainInvoice,
        latestMakeInvoiceTimeOfThisMonth:this.state.latestMakeInvoiceTimeOfThisMonth,
        latestMakeInvoiceTimeOfNextMonth:this.state.latestMakeInvoiceTimeOfNextMonth
      }
      for(let i in opt){
          if(opt[i]==null){
              opt[i] = "";
          }
      }
      console.log(opt)
      this.props.dispatch(InvoiceUpdate(token,opt));
      this.setState({
        alertstate:"toshow"
      })
    }
    this.fallbackParent = (name,value)=>{
      this.setState({[value]:name});
    }
    this.fallbackParent2 = (name,value)=>{
      this.setState({[name]:value});
    }
  }

  render() {
    return (
        <div className="col-sm-9 col-sm-offset-3 col-lg-10 col-lg-offset-2 main">			
          <div className="row">
            <ol className="breadcrumb">
              <li><a href="#"><span className="glyphicon glyphicon-home"></span></a></li>
              <li>发票信息</li>
            </ol>
          </div>
          <div className="row">
            <div className="col-lg-12">
              <div className="panel panel-default">
                <div className="panel-heading">
                  发票信息编辑
                  <Link className="pull-right btn btn-primary" style={{marginTop:"6px"}} to="/main/invoice">详情</Link>
                </div>
                <div className="panel-body">
                  <div className="form-horizontal">
                      <form>
                        <fieldset>
                        <SingleInput label="本月剩余发票" name="remainInvoice" placeholder="本月剩余发票" value={this.state.remainInvoice} backParent={this.fallbackParent2}/>
                        <DateInput label="本月最迟开票日" name="latestMakeInvoiceTimeOfThisMonth" value={this.state.latestMakeInvoiceTimeOfThisMonth} backParent={this.fallbackParent} tips="" />
                        <DateInput label="下月最早开票日" name="latestMakeInvoiceTimeOfNextMonth" value={this.state.latestMakeInvoiceTimeOfNextMonth} backParent={this.fallbackParent} tips="" />
                        <div className="box-btn">
                          <button type="submit" className="btn btn-primary" onClick={this.handlesubmit}>更改</button>
                        </div>
                      </fieldset>
                    </form>
                  </div>
                </div>
                <div  id="alertwrap" className={this.state.alertstate}>
                    <SweetAlert
                    info
                    confirmBtnText="确定"
                    confirmBtnBsStyle="primary"
                    title="发票信息已提交。"
                    onConfirm={()=>{this.setState({alertstate:"tohide"});history.push("/main/invoice")}}
                    >
                    </SweetAlert>
                </div>
              </div>
            </div>
          </div>	
        </div>
    )
  }
}

export default connect((state) => {
    const { authReducer,invoiceReducer } = state;
    return {
        authReducer,invoiceReducer
    }
})(InvoiceUpdateAll); 